/**
 * Perform the element default action.
 *
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"8178689B-FA13-426C-B207-A52A762D0A03"}
 * @AllowToRunInFind
 */
function buscar(event) 
{
	if (globals.marca_primer_find==0)
	{
		globals.marca_primer_find=1
		var estado_find=controller.find()
		if (estado_find==false)
		{
			globals.DIALOGS.showWarningDialog('Aviso','No hay movimientos de ctacte','OK')
		   	return;
		}
		cliente_id=globals.gClienteDesde;
		saldo = '!= 0';
		
		var found=controller.search()
		if (found==0)
		{
			globals.DIALOGS.showWarningDialog('Aviso','No hay movimientos de ctacte para ese cliente','OK')
		 	var $win = application.getWindow('imput_ctacte');
		 	$win.hide()
			$win.destroy()
		}
	}

}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"0E2ED7AD-E76C-4D41-B87B-A12BE97C38EC"}
 */
function inicio(firstShow, event) 
{
	globals.gtotal_creditos=0;
	globals.gtotal_debitos=0;
	globals.gtotal_comprob=0;
	buscar();
	totales();
	
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"135BB097-CCD9-4396-8924-928FFA4DADAC"}
 */
function totales(event) 
{
	globals.gtotal_creditos=0;
	globals.gtotal_debitos=0;
	globals.gtotal_comprob=0;
	var $cant = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $cant ; i++)
	{
		var $rcd = foundset.getRecord(i)
		if ($rcd.marcado_ctacte==1)
		{
			if ($rcd.ctacte_cl_to_tab_mov_cl.d_h==1)
				globals.gtotal_debitos += $rcd.saldo;
			else
				globals.gtotal_creditos += $rcd.saldo;
			globals.gtotal_comprob=globals.gtotal_debitos - globals.gtotal_creditos;
		}
	}
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"5BD2E66D-B594-4552-9FB1-A5D9B6FBA7AD"}
 */
function onDataChange_marcado_ctacte(oldValue, newValue, event) 
{
	totales();
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"F6E3089C-61D5-4DA8-9141-48DB1D257423"}
 */
function marcar_todo(event) 
{
	var $cant = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $cant ; i++)
	{
		var $rcd = foundset.getRecord(i)
		$rcd.marcado_ctacte = 1;
		if ($rcd.ctacte_cl_to_tab_mov_cl.d_h==1)
			globals.gtotal_debitos += $rcd.saldo;
		else
			globals.gtotal_creditos += $rcd.saldo;			
	}
	globals.gtotal_comprob=globals.gtotal_debitos - globals.gtotal_creditos;
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"428E626C-783A-4BC2-8A12-B96DCCD634AA"}
 */
function desmarcar_todo(event) 
{	
	var $cant = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $cant ; i++)
	{
		var $rcd = foundset.getRecord(i)
		$rcd.marcado_ctacte = 0;
	}
	globals.gtotal_creditos=0;
	globals.gtotal_debitos=0;
	globals.gtotal_comprob=0;
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"DD6B8C22-78E7-4300-8B3B-AF54B3CD3332"}
 */
function confirmar(event) 
{
	//crear los reg. de imput detalle
	
	//borrar  lo que tenga en imput_detalle 
	forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.deleteAllRecords()
	
	//recorre los marcados = 1
	var found = controller.getMaxRecordIndex()	
	if (found > 0)
	{
		globals.gtotal_comprob = 0 
		var aux_saldo = 0
		for (var i = 1 ; i <= controller.getMaxRecordIndex(); i++)
		{		 
			controller.setSelectedIndex(i)
			if (marcado_ctacte == 1)
			{
			//grabar lo que marcado en imput_detalle
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.newRecord()
			  
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.cod_mov		= cod_mov
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.fecha		= fecha_mov
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.importe		= saldo
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.imp_original	= saldo
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.nro_factura	= nro_factura
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.letra		= letra
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.sucursal		= sucursal
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.credito		= credito
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.debito		= debito
				forms.imput_cl_1.imput_cl_cabecera_to_imput_cl_detalle.ctacte_cl_id	= ctacte_cl_id
				if (ctacte_cl_to_tab_mov_cl.d_h == 2)
				{
				  	aux_saldo = saldo * (-1)
				}
				else
				{
				  	aux_saldo = saldo 
				} 
				globals.gtotal_comprob += aux_saldo
			}
		}
	}
	
	var $win = application.getWindow('imput_ctacte');
 	$win.hide()
	$win.destroy()
}
